package com.hk.kindheartedness;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.hk.kindheartedness.db.DBOpenHelper;
import com.hk.kindheartedness.util.DateUtil;
import com.hk.kindheartedness.util.RegexUtil;
import com.hk.kindheartedness.util.ToastUtil;

import java.util.Date;
import java.util.Random;

/**
 * 用户注册
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    //    Initialize Toast
    private Toast toast;

    //    Initialize intent
    private Intent intent;

    //    声明DBOpenHelper对象
    private DBOpenHelper dbOpenHelper;

    private EditText userName,loginPhone,idNumber,age,address,loginPwd,loginAgainPwd;

    private RadioButton man,woman;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
//        实例化
        dbOpenHelper = new DBOpenHelper(RegisterActivity.this, "db_kindheartedness", null, 1);
//        获取注册按钮控制权
        Button registerBtn = findViewById(R.id.registerBtn);
        registerBtn.setOnClickListener(this);
//      获取输入框的控制权
        userName = findViewById(R.id.userName);
        loginPhone = findViewById(R.id.loginPhone);
        idNumber = findViewById(R.id.idNumber);
        age = findViewById(R.id.age);
        address = findViewById(R.id.address);
        loginPwd = findViewById(R.id.loginPwd);
        loginAgainPwd = findViewById(R.id.loginAgainPwd);
        man = findViewById(R.id.man);
        woman = findViewById(R.id.woman);

    }

    /**
     * 注册按钮点击事件
     * @param v
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.registerBtn:
                addPatient();
                break;
            default:
                break;
        }
    }

    /**
     * 新增患者用户
     */
    private void addPatient() {
//        获取输入框内容
        String patientUserName = userName.getText().toString();
        String patientPhone = loginPhone.getText().toString();
        String patientIdNumber = idNumber.getText().toString();
        String patientAge = age.getText().toString();
        String patientAddress = address.getText().toString();
        String patientPwd = loginPwd.getText().toString();
        String patientAgainPwd = loginAgainPwd.getText().toString();
        //        判断选择的性别
        String patientSex = "0";
        if (woman.isChecked()) {
            patientSex = "1";
        }
//        用户类型
        String type = "0";
        long l = System.currentTimeMillis();
        String s = String.valueOf(l).substring(9);
        String yyyyMMdd = DateUtil.dateString(new Date(), "yyyyMMdd");
//        就诊卡
        String medicalCardNumber = yyyyMMdd+s;
        Boolean check = checkPatientData(patientUserName,patientPhone,patientIdNumber,patientAge,patientSex,patientAddress,patientPwd,patientAgainPwd);
        if (check) {
            SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
//            新增患者账户
            ContentValues contentValues = new ContentValues();
            contentValues.put("username",patientUserName);
            contentValues.put("phone",patientPhone);
            contentValues.put("password",patientPwd);
            contentValues.put("type",type);
            contentValues.put("medical_card_number",medicalCardNumber);
            contentValues.put("sex",patientSex);
            contentValues.put("age",patientAge);
            contentValues.put("id_number",patientIdNumber);
            contentValues.put("address",patientAddress);
            contentValues.put("create_time",DateUtil.dateString(new Date()));
            contentValues.put("update_time",DateUtil.dateString(new Date()));
            long insert = readableDatabase.insert("user", null, contentValues);
            if (insert > 0) {
                toast = ToastUtil.toastHint(toast,RegisterActivity.this,"注册成功，请登录！");
                intent = new Intent(RegisterActivity.this,LoginActivity.class);
                startActivity(intent);
                RegisterActivity.this.finish();
                return;
            }
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"注册失败，请稍后重试！");
            return;

        }


    }

    /**
     * 校验用户注册信息
     * @param patientUserName 姓名
     * @param patientPhone 手机号
     * @param patientIdNumber 身份证号
     * @param patientAge 年龄
     * @param patientSex 性别
     * @param patientAddress 地址
     * @param patientPwd 密码
     * @param patientAgainPwd 确认密码
     * @return
     */
    private Boolean checkPatientData(String patientUserName, String patientPhone, String patientIdNumber, String patientAge, String patientSex, String patientAddress, String patientPwd, String patientAgainPwd) {
        if (!RegexUtil.checkChinese(patientUserName)) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"姓名只支持中文！");
            return false;
        }
        if (!RegexUtil.checkMobile(patientPhone)) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请输入正确的手机号！");
            return false;
        }
        if (!RegexUtil.checkIdCard(patientIdNumber)) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请输入正确的身份证号！");
            return false;
        }
        if (!RegexUtil.checkDigitz(patientAge)) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请输入正确的年龄！");
            return false;
        }
        if (!patientSex.equals("0") && !patientSex.equals("1")) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请选择性别！");
            return false;
        }
        if (patientAddress.equals("")) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请输入地址！");
            return false;
        }

        if (patientPwd.equals("")) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"请输入密码！");
            return false;
        }

        if (!patientPwd.equals(patientAgainPwd)) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"密码输入不一致！");
            return false;
        }

//        校验手机号和身份证是否被注册
        SQLiteDatabase readableDatabase = dbOpenHelper.getReadableDatabase();
//        校验手机号
        Cursor query = readableDatabase.query("user", null, "phone = ?", new String[]{patientPhone}, null, null, null);
        String id = null;
        while (query.moveToNext()) {
            id = query.getString(0);
        }
        if (id != null) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"手机号已被注册，请更换");
            return false;
        }
//        校验身份证
        Cursor queryUser = readableDatabase.query("user", null, "id_number = ?", new String[]{patientIdNumber}, null, null, null);
        String id2 = null;
        while (queryUser.moveToNext()) {
            id2 = queryUser.getString(0);
        }
        if (id2 != null) {
            toast = ToastUtil.toastHint(toast,RegisterActivity.this,"身份证号已被注册，请更换");
            return false;
        }
        return true;
    }

    /**
     * 断开数据库连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (dbOpenHelper != null) {
            dbOpenHelper.close();
        }
    }

}